package com.xinchen.aiowaste.ui.adapter

import android.content.Context
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.maple.model.entity.aio.LabelListEntity
import com.maple.utils.ext.layoutInflater
import com.maple.utils.util.LogUtils
import com.maple.utils.util.UIUtils
import com.xinchen.aiowaste.R

class LabelListAdapter(val context: Context): RecyclerView.Adapter<LabelListAdapter.ViewHolder>(){

    private val list: MutableList<LabelListEntity.DataList> = mutableListOf()

    private var listener: OnClickListener? = null

    private val singularBg: Int = UIUtils.getColor(R.color.column_item_singular_bg)
    private val pluralBg: Int = UIUtils.getColor(R.color.column_item_plural_bg)

    private val icSelected: Drawable = UIUtils.getDrawable(com.maple.res.R.drawable.ic_checkbox_selected)
    private val icNormal: Drawable = UIUtils.getDrawable(com.maple.res.R.drawable.ic_checkbox_normal)
    fun setListener(l: OnClickListener?) {
        listener = l
    }


    fun onSelectedItem(pos: Int, item: LabelListEntity.DataList) {
        LogUtils.logGGQ("pos:${pos}")
        getDataList().forEachIndexed { index, dataList ->
            LogUtils.logGGQ("index:${index}")
            if(pos == index) {
                dataList.isSelected = !dataList.isSelected
            } else {
                dataList.isSelected = false
            }
        }
        this.notifyDataSetChanged()
    }


    fun getDataList(): List<LabelListEntity.DataList>{
        return list
    }

    fun setData(l: List<LabelListEntity.DataList>?) {
        if(!l.isNullOrEmpty()) {
            this.list.clear()
            this.list.addAll(l)
            this.notifyDataSetChanged()
        }
    }

    fun upDataList(l: List<LabelListEntity.DataList>?) {
        if(!l.isNullOrEmpty()) {
            this.list.addAll(l)
            this.notifyDataSetChanged()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LabelListAdapter.ViewHolder {
        val view: View = context.layoutInflater.inflate(R.layout.item_label_list, parent, false)
        val holder: ViewHolder = ViewHolder(view)
        holder.itemRoot.setOnClickListener {
            listener?.onItemClick(
                holder.bindingAdapterPosition,
                list.get(holder.bindingAdapterPosition),
            )
        }
        return holder
    }

    override fun onBindViewHolder(holder: LabelListAdapter.ViewHolder, position: Int) {
        holder.setData(position,list.get(position))
    }

    override fun getItemCount(): Int = list.size

    inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
        val itemRoot: LinearLayout = itemView.findViewById(R.id.item_root)
        val ivCheckBox: ImageView = itemView.findViewById(R.id.iv_checkbox)
        val tvCrateTime: TextView = itemView.findViewById(R.id.tv_create_time)
        val tvQrCode: TextView = itemView.findViewById(R.id.tv_qr_code)
        val tvWasteName: TextView = itemView.findViewById(R.id.tv_waste_name)
        val tvWasteCode: TextView = itemView.findViewById(R.id.tv_waste_code)
        val tvPrintStatus: TextView = itemView.findViewById(R.id.tv_print_status)
        val tvInStatus: TextView = itemView.findViewById(R.id.tv_in_status)
        val tvOutStatus: TextView = itemView.findViewById(R.id.tv_out_status)
        fun setData(position: Int,data: LabelListEntity.DataList) {
            itemRoot.setBackgroundColor(getItemBg(position))

            with(data) {
                tvCrateTime.text = "${UIUtils.getValue(createdAt)}"
                tvQrCode.text = "${UIUtils.getValue(qrCode)}"
                tvWasteName.text = "${UIUtils.getValue(wasteName)}"
                tvWasteCode.text = "${UIUtils.getValue(wasteCode)}"
                tvPrintStatus.text = getPrintStatus(isPrint)
                tvInStatus.text = getInStatus(isRuku)
                tvOutStatus.text = getOutStatus(isChuku)
                ivCheckBox.background = if(data.isSelected) icSelected else icNormal
            }
        }
    }

    private fun getItemBg(position: Int): Int {
        return if(position % 2 == 0) {
            singularBg
        } else {
            pluralBg
        }
    }

    private fun getPrintStatus(status: String?): String {
        return if(TextUtils.equals("1",status)) {
            "已打印"
        } else {
            "未打印"
        }
    }

    private fun getInStatus(status: String?): String {
        return if(TextUtils.equals("1",status)) {
            "已入库"
        } else {
            "未入库"
        }
    }

    private fun getOutStatus(status: String?): String {
        return if(TextUtils.equals("1",status)) {
            "已出库"
        } else {
            "未出库"
        }
    }

    fun getSelectedItem(): LabelListEntity.DataList?{
        return getDataList().findLast { it.isSelected }
    }

    interface OnClickListener {
        fun onItemClick(pos: Int = 0, item: LabelListEntity.DataList?)
    }

}